/**
 * Created by yangxianming on 2020/12/30.
 */
//调试代码
//var surveyList = ["首页效果","核心后管订单管理","企业后端订单详情","导出功能效果","移动端管理员易用性","移动端管理员易用性","移动端管理员易用性"];

//
//接口定义 都是以api开头 所需参数都通过方法传入
//function apiGetCorpList(dofun, envText){}
//

//获取问卷是否可用
function apiIsAvailableSurvey(){
    var uniqueId = getQueryVariable("uniqueid");
    var data = "action=checkAvailable&uniqueid=" + uniqueId;
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/survey/surveyUser?"+data, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    if(r["result"] == "0"){
                        apiGetSurveyList();
                    }else{
                        //alert("end");
                        window.location.href = "../image/sueveyend.png";
                    }
                }else{
                    //window.location.href = "../image/sueveyend.png";
                }
            }else{
                //window.location.href = "../image/sueveyend.png";
            }
        }
    };
    xmlHttp.send();
}

//获取问卷的详细信息
function apiGetSurveyList(){
    var surveyId = getQueryVariable("surveyid");
    var data = "action=getQuestionList&surveyId=" + surveyId;
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/survey/surveyUser?"+data, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    drawSurveyList(r["result"]["surveyname"], r["result"]["surveycontent"])
                }else{
                    alert("获取失败")
                }
            }
        }
    };
    xmlHttp.send();
}

//保存用户提交的问卷数据
function apiSaveUserData(){
    var name = prompt("请输入你的名字");
    var uniqueid = getQueryVariable("uniqueid");
    var surveyid = getQueryVariable("surveyid");
    //拿评分算平均
    var surveyLi = document.getElementById("surveyList").getElementsByTagName("li");
    var contentscore = [];
    var starCount = 0;
    var contentAverage = 0;
    for(var i=0; i<surveyLi.length; i++){
        var star = Number(surveyLi[i].getAttribute("star"));
        contentscore.push(star);
        starCount = starCount + star;
    }
    contentAverage = (starCount / surveyLi.length).toFixed(2);
    //用户文字描述
    var mark = document.getElementById("mark").value;
    var data = {
        "uniqueid":uniqueid,
        "surveyid":surveyid,
        "name":name,
        "contentScore":contentscore,
        "contentAverage":contentAverage,
        "mark":mark
    };
    var saveData = {
        "action":"saveUserAnswer",
        "data":data
    };
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "/survey/surveyUserSave", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    window.location.href = "../image/sueveyend.png";
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
                if (r["apiStatus"] == "-1"){
                    window.location.href = "../image/sueveyend.png";
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send(JSON.stringify(saveData));
}


//  -----------  页面绘制  -------------
//绘制问题列表
function drawSurveyList(surveyname, surveycontent){
    document.getElementById("name").innerText = surveyname;

    var surveyListUl = document.getElementById("surveyList");
    surveyListUl.innerText = "";
    for(var d=0; d<surveycontent.length; d++){
        var question = surveycontent[d];
        var li = document.createElement("li");
        li.setAttribute("style", "background-color: #ffffff; height: 200px;");
        li.style.marginBottom = "30px";
        li.setAttribute("star", "0");
        var div1 = document.createElement("div");
        div1.setAttribute("style", "text-align: center; width: 100%; font-size: 50px; color: #b2826c; margin-top: 20px");
        div1.innerText = question;
        li.appendChild(div1);
        drawStar(10, li);
        surveyListUl.appendChild(li);
    }
}

//星级
function drawStar(starCount, parentNode){
    var starSize = "50px";
    var startLeftMargin = "20px";

    var starDiv = document.createElement("div");
    starDiv.style.marginTop = "30px";
    starDiv.style.marginLeft = "60px";
    for(var si=0; si < starCount; si++){
        var starImg = document.createElement("img");
        starImg.style.float = "left";
        starImg.style.width = starSize;
        starImg.style.marginLeft = startLeftMargin;
        starImg.src = "../image/star1.jfif";//../image/star2.jfif
        starImg.onclick = function () {controlStar(this.parentNode, this)};
        starDiv.appendChild(starImg);
    }
    parentNode.appendChild(starDiv);
}
//星控, 高亮设置
function controlStar(starParent, thisStar){
    thisStar.setAttribute("flag", "1");
    var imgList = starParent.getElementsByTagName("img");
    var myLi = starParent.parentNode;
    var h = 1;
    for(var i=0; i<imgList.length; i++){
        var flag = imgList[i].getAttribute("flag");
        if(h == 0){
            imgList[i].src = "../image/star1.jfif";
            continue;
        }
        if(flag == "1"){
            h = 0;
            thisStar.removeAttribute("flag");
            myLi.setAttribute("star", (i+1).toString());
        }
        imgList[i].src = "../image/star2.jfif";

    }

}


//
//页面调用 数据获取，接口层与渲染层的连接
//


function initializeData(){
    apiIsAvailableSurvey();

}